<template>
  <div class="login-container">
    <h2 class="login-title">{{ t("registerPage.title") }}</h2>
    <el-form
      ref="registerForm"
      :model="form"
      :rules="rules"
      label-width="0"
      @submit.prevent="onSubmit"
      class="login-form"
    >
      <el-form-item prop="username">
        <el-input
          v-model="form.username"
          :placeholder="t('registerPage.usernamePlaceholder')"
          :prefix-icon="User"
          size="large"
          class="login-username"
        />
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="form.password"
          type="password"
          :placeholder="t('registerPage.passwordPlaceholder')"
          show-password
          :prefix-icon="Lock"
          size="large"
          class="login-password"
        />
      </el-form-item>
      <el-form-item prop="repassword">
        <el-input
          v-model="form.repassword"
          type="password"
          :placeholder="t('registerPage.confirmPasswordPlaceholder')"
          show-password
          :prefix-icon="Lock"
          size="large"
          class="login-password"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" class="login-button" native-type="submit">
          {{ t("registerPage.registerButton") }}
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue";
import { User, Lock } from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";
import { registerApi } from "../api";
import { useRouter } from "vue-router";
import { useI18n } from "vue-i18n";

const { t } = useI18n();

// 表单数据
const form = ref({
  username: "",
  password: "",
  repassword: "",
});
const router = useRouter();
// 自定义确认密码验证规则
const validateRepassword = (rule, value, callback) => {
  if (value === "") {
    callback(new Error(t("registerPage.rules.confirmPassword")));
  } else if (value !== form.value.password) {
    callback(new Error(t("registerPage.rules.samePassword")));
  } else {
    callback();
  }
};

// 表单验证规则
const rules = reactive({
  username: [
    {
      required: true,
      message: t("registerPage.rules.username"),
      trigger: "blur",
    },
    {
      min: 3,
      max: 12,
      message: t("registerPage.rules.usernameLength"),
      trigger: "blur",
    },
  ],
  password: [
    {
      required: true,
      message: t("registerPage.rules.password"),
      trigger: "blur",
    },
    {
      min: 6,
      max: 16,
      message: t("loginPage.rules.passwordLength"),
      trigger: "blur",
    },
  ],
  repassword: [
    { required: true, validator: validateRepassword, trigger: "blur" },
  ],
});

// 表单引用
const registerForm = ref(null);

// 提交表单
const onSubmit = () => {
  registerForm.value.validate((valid) => {
    if (valid) {
      // 表单验证通过，执行注册逻辑
      try {
        const res = registerApi(form.value);
        router.push("/login");
      } catch (error) {
        form.value.username = "";
        form.value.password = "";
        form.value.repassword = "";
      }
    } else {
      // 表单验证失败
      ElMessage.warning(t("registerPage.checkWarning"));
    }
  });
};
</script>
